package com.zmx.procurementmall.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.gyf.immersionbar.ImmersionBar;
import com.zmx.procurementmall.MyApplication;
import com.zmx.procurementmall.R;
import com.zmx.procurementmall.okhttp.OkHttp3ClientManager;
import com.zmx.procurementmall.okhttp.UrlConfig;
import com.zmx.procurementmall.utils.MySharedPreferences;
import com.zmx.procurementmall.view.activity.LoginActivity;
import com.zmx.procurementmall.view.activity.OrderManagementActivity;
import com.zmx.procurementmall.view.activity.UserDataActivity;

import java.util.HashMap;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;

public class FourFragment extends SimpleImmersionFragment {

    private CircleImageView head;
    private TextView all_order,consignee,shop_name;
    private RelativeLayout layout_a,layout_b,layout_c,layout_d;
    private Button log_ut;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_four, container, false);

        consignee = view.findViewById(R.id.consignee);
        shop_name = view.findViewById(R.id.shop_name);
        log_ut = view.findViewById(R.id.log_ut);
        log_ut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                MySharedPreferences.getInstance(FourFragment.this.getContext()).clear();
                shop_name.setText("请登录");
                log_ut.setVisibility(View.GONE);
                head.setBackgroundResource(R.mipmap.ic_morentu);
                consignee.setText("");

            }
        });

        layout_a = view.findViewById(R.id.layout_a);
        layout_a.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (MyApplication.isLogin_state()){

                    Intent intent = new Intent();
                    intent.putExtra("logo","1");
                    intent.setClass(FourFragment.this.getContext(), OrderManagementActivity.class);
                    startActivity(intent);

                }else{

                    Intent intent = new Intent();
                    intent.setClass(FourFragment.this.getContext(), LoginActivity.class);
                    startActivity(intent);

                }
            }
        });
        layout_b = view.findViewById(R.id.layout_b);
        layout_b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (MyApplication.isLogin_state()){

                    Intent intent = new Intent();
                    intent.putExtra("logo","2");
                    intent.setClass(FourFragment.this.getContext(), OrderManagementActivity.class);
                    startActivity(intent);

                }else{

                    Intent intent = new Intent();
                    intent.setClass(FourFragment.this.getContext(), LoginActivity.class);
                    startActivity(intent);

                }
            }
        });
        layout_c = view.findViewById(R.id.layout_c);
        layout_c.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (MyApplication.isLogin_state()){

                    Intent intent = new Intent();
                    intent.putExtra("logo","3");
                    intent.setClass(FourFragment.this.getContext(), OrderManagementActivity.class);
                    startActivity(intent);

                }else{

                    Intent intent = new Intent();
                    intent.setClass(FourFragment.this.getContext(), LoginActivity.class);
                    startActivity(intent);

                }
            }
        });
        layout_d= view.findViewById(R.id.layout_d);
        layout_d.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (MyApplication.isLogin_state()){

                    Intent intent = new Intent();
                    intent.putExtra("logo","0");
                    intent.setClass(FourFragment.this.getContext(), OrderManagementActivity.class);
                    startActivity(intent);

                }else{

                    Intent intent = new Intent();
                    intent.setClass(FourFragment.this.getContext(), LoginActivity.class);
                    startActivity(intent);

                }
            }
        });

        head = view.findViewById(R.id.head);
        head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (MyApplication.isLogin_state()) {


                    Intent intent = new Intent();
                    intent.setClass(FourFragment.this.getContext(), UserDataActivity.class);
                    startActivity(intent);

                }else{
                        Intent intent = new Intent();
                        intent.setClass(FourFragment.this.getContext(), LoginActivity.class);
                        startActivity(intent);
                }

            }
        });

        all_order = view.findViewById(R.id.all_order);
        all_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (MyApplication.isLogin_state()){

                    Intent intent = new Intent();
                    intent.putExtra("logo","0");
                    intent.setClass(FourFragment.this.getContext(), OrderManagementActivity.class);
                    startActivity(intent);

                }else{

                    Intent intent = new Intent();
                    intent.setClass(FourFragment.this.getContext(), LoginActivity.class);
                    startActivity(intent);

                }


            }
        });

        return view;

    }

    @Override
    public void initImmersionBar() {

        ImmersionBar.with(this).titleBarMarginTop(R.id.tool_bar).statusBarDarkFont(true).init();

    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){

                case 1:
                    Log.e("数据","数据"+msg.obj.toString());
                    break;

            }

        }
    };


    @Override
    public void onResume() {
        super.onResume();

        if (MyApplication.isLogin_state()){

            log_ut.setVisibility(View.VISIBLE);
            shop_name.setText("店长："+MySharedPreferences.getInstance(this.getContext()).getString(MySharedPreferences.consignee,""));
            consignee.setText("店铺："+MySharedPreferences.getInstance(this.getContext()).getString(MySharedPreferences.shop_name,""));
            Glide.with(FourFragment.this.getContext()).load(UrlConfig.IMAGEURL+ MySharedPreferences.getInstance(FourFragment.this.getContext()).getString(MySharedPreferences.heading,"")).into(head);

        }else{

            log_ut.setVisibility(View.GONE);
            shop_name.setText("请登录");
            consignee.setText("");

        }

    }

    //更新购物车某个商品数量
    public void updateGoodsNum(Integer uid){

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("uid", uid);

        OkHttp3ClientManager.getInstance().NetworkRequestMode(UrlConfig.SELECT_ALL_ORDER_INFO, params, handler, 1, 404);


    }

}